#include "sraccountmanagewidget.h"
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QRegExpValidator>

#include "srdatabase.h"
#include <OEMessageBox>
#include <OEInputBox>

// private lib
#include <auditcore.h>

SRAccountManageWidget::SRAccountManageWidget(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout* layout = new QVBoxLayout();
    QHBoxLayout* layout_user = new QHBoxLayout();
    QHBoxLayout* layout_name = new QHBoxLayout();
    QHBoxLayout* layout_pswd = new QHBoxLayout();
    QHBoxLayout* layout_pswd_ = new QHBoxLayout();
    // logo
    QGridLayout* layout_logo = new QGridLayout();


    QWidget* wdgt_logo = new QWidget();
    QLabel* lab_logo = new QLabel();
    lab_logo->setAlignment(Qt::AlignCenter);
    lab_logo->setPixmap(QPixmap(":/lab/logo"));
    QLabel* lab_logo_title = new QLabel("账号管理");
    lab_logo_title->setAlignment(Qt::AlignCenter);
    lab_logo_title->setObjectName(QString("LogoTitle"));

    labAcctionUser_ = new QLabel();
    labAcctionUserName_ = new QLabel();
    editAcctionPswd__ = new QLineEdit();
    editAcctionPswd_ = new QLineEdit();

    labAcctionUser_->setMinimumWidth(100);
    labAcctionUserName_->setMinimumWidth(100);
    editAcctionPswd__->setMinimumWidth(100);
    editAcctionPswd_->setMinimumWidth(100);

    // 输入限制规则
    QRegExp regQuote("[0-9a-zA-Z]{1,12}");
    QRegExpValidator* quoteValidator = new QRegExpValidator(regQuote, this);

    editAcctionPswd__->setValidator(quoteValidator);
    editAcctionPswd_->setValidator(quoteValidator);
    editAcctionPswd_->setEchoMode(QLineEdit::EchoMode::Password);
    editAcctionPswd__->setEchoMode(QLineEdit::EchoMode::Password);

    QLabel* lab_acction_name = new QLabel(tr("用户姓名"));
    QLabel* lab_acction_user = new QLabel(tr("账　　号"));
    QLabel* lab_acction_pswd = new QLabel(tr("登录密码"));
    QLabel* lab_acction_pswd_ = new QLabel(tr("确认密码"));

    lab_acction_user->setAlignment(Qt::AlignCenter);
    lab_acction_name->setAlignment(Qt::AlignCenter);
    lab_acction_pswd->setAlignment(Qt::AlignCenter);
    lab_acction_pswd_->setAlignment(Qt::AlignCenter);
    labAcctionUser_->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    labAcctionUserName_->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    QPushButton* btn_modify_name = new QPushButton(tr("修改"));
    btn_modify_name->hide();
    QPushButton* btn_modify_pswd = new QPushButton(tr("保存"));

    layout_name->addWidget(lab_acction_name);
    layout_name->addWidget(labAcctionUserName_);

    layout_user->addWidget(lab_acction_user);
    layout_user->addWidget(labAcctionUser_);


    layout_pswd->addWidget(lab_acction_pswd);
    layout_pswd->addWidget(editAcctionPswd_);

    layout_pswd_->addWidget(lab_acction_pswd_);
    layout_pswd_->addWidget(editAcctionPswd__);


    QVBoxLayout* main_layout = new QVBoxLayout();
    QWidget* wdgt_main = new QWidget();

    btn_modify_name->setFixedSize(44,30);
    btn_modify_pswd->setFixedSize(44,30);
    labAcctionUser_->setFixedSize(160,30);
    labAcctionUserName_->setFixedSize(160,30);
    editAcctionPswd__->setFixedSize(160,30);
    editAcctionPswd_->setFixedSize(160,30);
//    labAcctionUser_->setFixedSize(210,30);

    wdgt_main->setFixedSize(325,290);


    layout_logo->addWidget(lab_logo,0,1,1,1);
    layout_logo->addWidget(lab_logo_title,1,1,1,1);
    wdgt_logo->setLayout(layout_logo);
    layout->setMargin(0);
    layout->addWidget(wdgt_logo);
    layout->addLayout(layout_user);
    layout->addLayout(layout_name);
    layout->addLayout(layout_pswd);
    layout->addLayout(layout_pswd_);
    layout->addWidget(btn_modify_pswd, 0, Qt::AlignRight);
//    layout->addStretch();
    wdgt_main->setLayout(layout);
    main_layout->addWidget(wdgt_main,0,Qt::AlignCenter);

    setLayout(main_layout);


    // link
    {
        connect(btn_modify_name, &QPushButton::clicked,
                this, &SRAccountManageWidget::onAccountNameModify);
        connect(btn_modify_pswd, &QPushButton::clicked,
                this, &SRAccountManageWidget::onAccountPswdModify);
    }

}

void SRAccountManageWidget::setUser(const QString &_user,
                                    const QString &_name)
{
    labAcctionUser_->setText(_user);

    labAcctionUserName_->setText(_name);
}

void SRAccountManageWidget::onAccountNameModify()
{

//    if (editAcctionPswd__->text().isEmpty()) {
//        // 提示成功
//        OEMessageBox::BriefMessage(this, "名字不能为空");
//        return;
//    }

//    // 获得超级管理员的密码
//    char ch_super_pswd[16] = {};


//    int ret = apiModifyAccountName(labAcctionUser_->text().toUtf8().data(),
//                         editAcctionPswd__->text().toUtf8().data(),
//                         ch_super_pswd);

//    if (ret == 0) {
//        // 提示成功
//        SRDatabase::DB()->writeOperateLog(QString("更改姓名为%1").arg(editAcctionPswd__->text()));
//        editAcctionPswd__->setPlaceholderText(editAcctionPswd__->text());
//        editAcctionPswd__->clear();
//        OEMessageBox::BriefMessage(this, "修改成功");
//    }
//    else {
//        // 提示失败
//        OEMessageBox::BriefMessage(this, "修改失败");
//    }
}


void SRAccountManageWidget::onAccountPswdModify() {
    if (editAcctionPswd_->text().isEmpty()
        || editAcctionPswd__->text().isEmpty()) {
        OEMessageBox::BriefMessage(this, tr("密码不能为空"));
        return;
    }
    else if (editAcctionPswd_->text().length() < 6
        || editAcctionPswd__->text().length() < 6) {
        OEMessageBox::BriefMessage(this, tr("密码不能小于6位"));
        return;
    }
    else if (editAcctionPswd_->text() != editAcctionPswd__->text()) {
        OEMessageBox::BriefMessage(this, tr("输入的密码不一致"));
        return;
    }
    int ret = apiModifyAccountPswd(labAcctionUser_->text().toUtf8().data(),
                                 editAcctionPswd_->text().toUtf8().data(),
                                 "ADMIN");

    if (ret == 0) {
        OEMessageBox::BriefMessage(this, "密码修改成功");
        editAcctionPswd_->clear();
        editAcctionPswd__->clear();
    }
    else {
        OEMessageBox::BriefMessage(this, "修改失败");
    }
//    // 获得超级管理员的密码
//    OEInputBox* w = new OEInputBox;
//    w->setEditEchoMode(QLineEdit::EchoMode::Password);
//    w->setEditLabel("主管密码：");
//    w->setEditPlaceholderText("请输入6 - 12位英文或数字密码");
//    w->resize(400,300);
//    connect(w,&OEInputBox::sigInputMsg,
//            this,[=](const QString& super_pswd){
//        if (super_pswd.isEmpty()) {
//            OEMessageBox::BriefMessage(this, "主管密码不能为空");
//        }


//        int ret = apiModifyAccountPswd(labAcctionUser_->text().toUtf8().data(),
//                                       editAcctionPswd_->text().toUtf8().data(),
//                                       super_pswd.toUtf8().data());

//        if (ret == 0) {
//            // 提示成功
//            SRDatabase::DB()->writeOperateLog(QString("修改了密码"));
//            OEMessageBox::BriefMessage(this, "密码修改成功");
//            editAcctionPswd_->setText("");
//        }
//        else {
//            // 提示失败
//            OEMessageBox::BriefMessage(this, "主管密码不正确");
//        }
//    });
//    w->exec();
}
